package site.lanmushan.generate.util;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.FileWriter;
import java.util.Map;

public class Test {

    public static void main(String[] args) {
        VelocityContext context = new VelocityContext();

    }

    private static void codeGenerator(VelocityContext context, Map<String, String> codeTemplates) throws Exception {
        String projectPath = "D:\\temp";

        Velocity.setProperty("input.encoding", "UTF-8");
        Velocity.setProperty("output.encoding", "UTF-8");

        for (Map.Entry<String, String> entry : codeTemplates.entrySet()) {
            String template = entry.getKey();
            String filePath = projectPath + entry.getValue();
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator) + 1);
            String fileDir = filePath.replace(fileName, "");
            File directory = new File(fileDir);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            FileWriter writer;
            try {
                writer = new FileWriter(filePath);
                Template tpl = Velocity.getTemplate(template, "UTF-8");
                tpl.merge(context, writer);
                writer.flush();
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
